/* csrf_token */
var token = $('meta[name=csrf-token]').attr('content');

$.ajaxSetup({
    headers: {
        "X-CSRF-TOKEN": token,
    }
});

var editor = new wangEditor('content');

if (editor && editor.config) {
    editor.config.uploadImgUrl = '/posts/image/upload';

    // 设置 headers（举例）
    editor.config.uploadHeaders = {
        'X-CSRF-TOKEN' : token,
    };

    editor.create();
}

// 上传图片预览
$(".preview_input").change(function (event) {
    var file = event.currentTarget.files[0];
    if (!file) {
        return console.log('no file found');
    }
    var url = window.URL.createObjectURL(file);
    $(event.target).next(".preview_img").attr("src", url);
});


// 关注,取消关注用户
$('.like-button').click(function (event) {
    var target = $(event.target);
    var current_like = target.attr('like-value');
    var user_id = target.attr('like-user');
    if (current_like == 1) {
        // 已经关注,取消关注
        $.ajax({
            url: '/user/' + user_id + '/unfan',
            method: 'post',
            dataType: 'json',
            success: function (data) {
                if (data.code !== 0) {
                    return alert("错误:" + data.msg);
                }

                target.addClass("btn-success");
                target.attr("like-value", 0);
                target.text("关注");
            }
        })
    } else {
        // 没有关注,关注
        $.ajax({
            url: '/user/' + user_id + '/fan',
            method: 'post',
            dataType: 'json',
            success: function (data) {
                if (data.code !== 0) {
                    return alert("错误:" + data.msg);
                }

                target.removeClass("btn-success");
                target.attr("like-value", 1);
                target.text("取消关注");
            }
        })
    }
});
